package com.newbingo.util;

import java.io.Serializable;
import java.util.List;

import com.newbingo.entity.Game;
import com.newbingo.entity.GamePlay;
import com.newbingo.entity.GameTable;

/**
 * Game树型类
 * @author ChansonYan
 *
 */
public class GameTree implements Serializable {
	
	/**
	 * 查询出游戏的集合，包括了游戏桌的信息,根据游戏的集合进行JSON转换
	 * @param games
	 * @return
	 */
	public static String getGamesJson(List<Game> games){
		StringBuffer sb=new StringBuffer();
		sb.append("[");
		//首先循环出游戏，添加入StringBuffer中
		for(Game game :games){
			sb.append("{");
			sb.append("id:"+game.getGameno()+",");
			sb.append("text:'"+game.getGamename()+"',");
			sb.append("name:'"+game.getGamename()+"'");
			//判断此游戏是否有游戏桌，从而添加子节点
			if(game.getGtable()!=null&&game.getGtable().size()>0){
				sb.append(",children:[");
				//循环出游戏桌，添加到子节点中
				for(GameTable gt:game.getGtable()){
					sb.append("{tid:"+gt.getGtno()+",");
					sb.append("text:'"+gt.getName()+"',");
					sb.append("name:'"+gt.getName()+"',");
					sb.append("leaf:true},");
				}
				sb.deleteCharAt(sb.length()-1);
				sb.append("]");
			}else{
				sb.append(",leaf:true");
			}
			sb.append("},");
		}
		sb.deleteCharAt(sb.length()-1);
		sb.append("]");
		System.out.println(sb.toString());
		return sb.toString();
	}
	
	public static String getGamePlaysJson(List<Game> games){
		StringBuffer sb=new StringBuffer();
		sb.append("[");
		//首先循环出游戏，添加入StringBuffer中
		for(Game game :games){
			sb.append("{");
			sb.append("id:"+game.getGameno()+",");
			sb.append("text:'"+game.getGamename()+"',");
			sb.append("name:'"+game.getGamename()+"'");
			//判断此游戏是否有游戏玩法，从而添加子节点
			if(game.getPlays()!=null&&game.getPlays().size()>0){
				sb.append(",children:[");
				//循环出游戏玩法，添加到子节点中
				for(GamePlay gp:game.getPlays()){
					sb.append("{pid:"+gp.getId()+",");
					sb.append("text:'"+gp.getName()+"',");
					sb.append("name:'"+gp.getName()+"',");
					sb.append("leaf:true},");
				}
				sb.deleteCharAt(sb.length()-1);
				sb.append("]");
			}else{
				sb.append(",leaf:true");
			}

			sb.append("},");
		}
		sb.deleteCharAt(sb.length()-1);
		sb.append("]");
		return sb.toString();
	}
	
	

}
